import 'package:flutter/material.dart';
import 'widget_single_input_item.dart';

/// 单行输入，不需要controller
/// [title]:标题
/// [hintText]:提示文字
/// [initialValue]:初始显示的文字
/// [maxLength]:最大的文字数量
/// [hasLine]：是否有下划线
/// [onChanged]:输入变化时的回调
/// [inputCharFormatterType]:输入内容的类型
/// [allowEmpty]:是否必填，false时显示必填辨识
/// [unitText]：不为空显示单位
class SingleInputWidget extends StatefulWidget {
  final String title;
  final String hintText;
  final String initialValue;
  final int maxLength;
  final bool hasLine;
  final ValueChanged<String> onChanged;
  final InputCharFormatterType inputCharFormatterType;
  final bool allowEmpty;
  final String unitText;

  SingleInputWidget(
      {this.title = '',
      this.hintText = '',
      this.initialValue,
      this.maxLength = 100,
      this.hasLine = true,
      this.onChanged,
      this.allowEmpty = true,
      this.inputCharFormatterType =
          InputCharFormatterType.InputCharFormatterNone,
      this.unitText = "",});

  @override
  _SingleInputState createState() => _SingleInputState();
}

class _SingleInputState extends State<SingleInputWidget> {
  TextEditingController controller;
  @override
  void initState() {
    super.initState();
    controller = TextEditingController();
    controller.text = widget.initialValue;
    controller.addListener(() {
      if (widget.onChanged != null) {
        widget.onChanged(controller.text);
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return SingleInputItemWidget(
      title: widget.title,
      placeholder: widget.hintText,
      maxLength: widget.maxLength,
      isLine: widget.hasLine,
      controller: controller,
      inputCharFormatterType: widget.inputCharFormatterType,
      enableInteractiveSelection: true,
      allowEmpty:widget.allowEmpty,
      unitText:widget.unitText,
    );
  }

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }
}
